package rpg.role
{
	import rpg.Utils.Log;
	import rpg.core.GameBase;
	import rpg.core.GameComponent;
	import rpg.core.RenderComponent;
	import rpg.render.RenderData;

	/**
	 * 游戏中场景对象基类
	 * 角色,NPC,怪物,掉落物品
	 * @author hyy
	 *
	 */
	public class SceneObject extends GameBase
	{
		/**
		 * 物品的名称
		 */
		public var name : String;
		/**
		 * 所在地图的位置
		 */
		public var mapX : int;
		public var mapY : int;
		/**
		 * 宽高
		 */
		public var width : int;
		public var height : int;
		/**
		 * 需要行走的目标位置
		 */
		public var targetMapX : int;
		public var targetMapY : int;
		protected var _alpha : Number;
		protected var _visible : Boolean = true;
		protected var _mouseEnabled : Boolean = true;
		protected var _blendMode : String;

		protected var renderList : Array = [];
		protected var mainRender : RenderData;

		public function SceneObject(id : String)
		{
			super(id);
			mainRender = new RenderData();
			mainRender.addShow();
		}

		override public function update(elapsedTime : uint) : void
		{
			mainRender.x = mapX;
			mainRender.y = mapY;
			super.update(elapsedTime);
		}

		override public function addComponent(com : GameComponent , priority : int = 0) : void
		{
			super.addComponent(com , priority);
			if(com is RenderComponent)
			{
				addRender(RenderComponent(com).render);
			}
		}

		override public function removeComponent(com : GameComponent) : void
		{
			super.removeComponent(com);
			if(com is RenderComponent)
			{
				removeRender(RenderComponent(com).render);
			}
		}

		public function addRender(render : RenderData) : void
		{
			var index : int = renderList.indexOf(render);
			if(index == -1)
			{
				renderList.push(render);
				mainRender.addChild(render);
			}
		}

		public function removeRender(render : RenderData) : void
		{
			var index : int = renderList.indexOf(render);
			if(index != -1)
			{
				renderList.splice(index , 1);
				mainRender.removeChild(render);
				render.removeShow();
			}
		}

		/**
		 * 混合模式
		 */
		public function get blendMode() : String
		{
			return _blendMode;
		}

		/**
		 * @private
		 */
		public function set blendMode(value : String) : void
		{
			_blendMode = value;
		}

		/**
		 * 透明度
		 */
		public function get alpha() : Number
		{
			return _alpha;
		}

		/**
		 * @private
		 */
		public function set alpha(value : Number) : void
		{
			_alpha = value;
		}

		/**
		 * 是否禁用鼠标事件
		 */
		public function get mouseEnabled() : Boolean
		{
			return _mouseEnabled;
		}

		/**
		 * @private
		 */
		public function set mouseEnabled(value : Boolean) : void
		{
			_mouseEnabled = value;
		}

		/**
		 * 是否可见
		 */
		public function get visible() : Boolean
		{
			return _visible;
		}

		/**
		 * @private
		 */
		public function set visible(value : Boolean) : void
		{
			_visible = value;
		}

		override public function destroy() : void
		{
			Log.print("销毁:" , name);
			super.destroy();
			renderList.length = 0;
			renderList = null;
			mainRender.destory();
			mainRender = null;
		}

	}
}